// /////////////////////////////////////////////////////////////////////////////
//
// Name:            asHasEvents.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Binds the HasEvents class to AngelScript
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "asHasEvents.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "ScriptedEvent.h"

//! Wrapper function to allow script-side classes to connect event signals.
//! @note For internal use only!
bool connectEventSignal(const std::string &slotName, asIScriptObject *object,
                               const std::string &function, HasEvents *container)
{
    // Create a ScriptedEvent instance.
    ScriptedEvent *scriptedEvent = new ScriptedEvent(object, function);

    // Connect our wrapper function to the appropriate slots.
    if(container->connectEventSignal(slotName, scriptedEvent, &ScriptedEvent::onEvent))
    {
       // Set up the disconnect slot.
       std::stringstream ss;
       ss << slotName << "#disconnect[" << object->GetTypeId() << "-"
          << object->GetObjectType()->GetMethodIdByName(function.c_str()) << "]";

       container->createEventSlot(ss.str());
       container->connectEventSignal(ss.str(), scriptedEvent, &ScriptedEvent::onDisconnect);

       // We've succeeded.
       return true;
    }

    else
    {
       scriptedEvent->drop();
       return false;
    }
}

//! Wrapper function to allow script-side classes to disconnect event signals.
//! @note For internal use only!
void disconnectEventSignal(const std::string &slotName, asIScriptObject *object,
                           const std::string &function, HasEvents *container)
{
    // Emit the disconnect event.
    std::stringstream ss;
    ss << slotName << "#disconnect[" << object->GetTypeId() << "-"
       << object->GetObjectType()->GetMethodIdByName(function.c_str()) << "]";

    container->emitEvent(ss.str());

    // Remove the disconnect slot.
    container->removeEventSlot(ss.str());
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

// End of File
